using System;
using Firestarter.Test.Base;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.BO;
using NUnit.Framework;

namespace FireStarterModeller.Test.BO
{
    [TestFixture]
    public class TestDMObjectIdentityAllPropsCompulsoryRule
    {
        [SetUp]
        public void SetupTest()
        {
            //Runs every time that any testmethod is executed
            TestUtilsDomain.SetupTest();
            BORegistry.DataAccessor = new DataAccessorInMemory();
        }

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            //Code that is executed before any test is run in this class. If multiple tests
            // are executed then it will still only be called once.
            TestUtilsDomain.SetupFixture();
        }

        [TearDown]
        public void TearDownTest()
        {
            //runs every time any testmethod is complete
        }

        #region TestDMObjectIdentityAllPropsCompulsoryRule

        [Test]
        public void Test_CreateDMObjectIdentityAllPropsCompulsoryRule()
        {
            //---------------Set up test pack-------------------
            DMObjectIdentity dmObjectIdentity = TestUtilsShared.CreateUnsavedValidObjectIdentityInfo();

            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            IBusinessObjectRule rule = new DMObjectIdentityAllPropsCompulsoryRule();
            //---------------Test Result -----------------------
            Assert.IsNotNull(rule);
            Assert.AreEqual("DMObjectIdentityAllPropsCompulsory", rule.Name);
            Assert.AreEqual(ErrorLevel.Error, rule.ErrorLevel);
        }

//        [Test]
//        public void Test_CreateDMObjectIdentityAllPropsCompulsoryRule_WhenDMObjectIdentityNull_ShouldRaiseError()
//        {
//            //---------------Set up test pack-------------------
//            //---------------Assert Precondition----------------
//            //---------------Execute Test ----------------------
//            try
//            {
//                new DMObjectIdentityAllPropsCompulsoryRule(null);
//                Assert.Fail("expected ArgumentNullException");
//            }
//            //---------------Test Result -----------------------
//            catch (ArgumentNullException ex)
//            {
//                StringAssert.Contains("Value cannot be null", ex.Message);
//                StringAssert.Contains("dmObjectIdentity", ex.ParamName);
//            }
//        }

        [Test]
        public void Test_DMObjectIdentityAllPropsCompulsoryRule_FailsWhenPropForObjectIdentityIsNotCompulsory()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            DMProperty property = dmClass.Properties.CreateBusinessObject();
            DMObjectIdentity dmObjectIdentity = dmClass.CreateObjectIdentity(property);
            property.Compulsory = false;
            IBusinessObjectRule rule = new DMObjectIdentityAllPropsCompulsoryRule();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmClass.ObjectIdentity);
            Assert.AreEqual(1, dmObjectIdentity.Properties.Count);
            Assert.IsFalse(property.Compulsory.Value);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmObjectIdentity);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
            StringAssert.Contains("All the properties that form the DMObjectIdentity for Entity/Class '", rule.Message);
            StringAssert.Contains("'" + dmObjectIdentity.Class.ToString() + "'", rule.Message);
            StringAssert.Contains("' must be compulsory.", rule.Message);
        }


        [Test]
        public void Test_DMObjectIdentityAllPropsCompulsoryRule_FailsWhenPropForObjectIdentityIsNotCompulsory_MultipleProps()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            DMProperty property1 = dmClass.Properties.CreateBusinessObject();
            DMProperty property2 = dmClass.Properties.CreateBusinessObject();
            DMObjectIdentity dmObjectIdentity = dmClass.CreateObjectIdentity(property1);
            dmObjectIdentity.Properties.Add(property2);
            property2.Compulsory = false;
            IBusinessObjectRule rule = new DMObjectIdentityAllPropsCompulsoryRule();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmClass.ObjectIdentity);
            Assert.AreEqual(2, dmObjectIdentity.Properties.Count);
            Assert.IsTrue(property1.Compulsory.Value);
            Assert.IsFalse(property2.Compulsory.Value);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmObjectIdentity);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
            TestUtilsShared.AssertStringNotEmpty(rule.Message, "rule.Message");
        }

        [Test]
        public void Test_DMObjectIdentityAllPropsCompulsoryRule_PassesWhenPropForObjectIdentityIsCompulsory()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            DMProperty prop = dmClass.Properties.CreateBusinessObject();
            DMObjectIdentity dmObjectIdentity = dmClass.ObjectIdentity = new DMObjectIdentity();
            dmClass.ObjectIdentity.Properties.Add(prop);
            IBusinessObjectRule rule = new DMObjectIdentityAllPropsCompulsoryRule();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmClass.ObjectIdentity);
            Assert.AreEqual(1, dmObjectIdentity.Properties.Count);
            Assert.IsTrue(prop.Compulsory.Value);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmObjectIdentity);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_DMObjectIdentityAllPropsCompulsoryRule_PassesWhenPropForObjectIdentityIsAutoIncrementingAndNotCompulsory()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            DMProperty prop = dmClass.Properties.CreateBusinessObject();
            prop.AutoIncrementing = true;
            prop.Compulsory = false;
            DMObjectIdentity dmObjectIdentity = dmClass.ObjectIdentity = new DMObjectIdentity();
            dmClass.ObjectIdentity.Properties.Add(prop);
            IBusinessObjectRule rule = new DMObjectIdentityAllPropsCompulsoryRule();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmClass.ObjectIdentity);
            Assert.AreEqual(1, dmObjectIdentity.Properties.Count);
            Assert.IsTrue(prop.AutoIncrementing.Value);
            Assert.IsFalse(prop.Compulsory.Value);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmObjectIdentity);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_DMObjectIdentityAllPropsCompulsoryRule_PassesWhenPropForObjectIdentityIsCompulsory_MultipleProps()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            DMProperty property1 = dmClass.Properties.CreateBusinessObject();
            DMProperty property2 = dmClass.Properties.CreateBusinessObject();
            DMObjectIdentity dmObjectIdentity = dmClass.CreateObjectIdentity(property1);
            dmObjectIdentity.Properties.Add(property2);
            IBusinessObjectRule rule = new DMObjectIdentityAllPropsCompulsoryRule();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmClass.ObjectIdentity);
            Assert.AreEqual(2, dmObjectIdentity.Properties.Count);
            Assert.IsTrue(property1.Compulsory.Value);
            Assert.IsTrue(property2.Compulsory.Value);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmObjectIdentity);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_DMObjectIdentityAllPropsCompulsoryRule_FailsWhenPropForObjectIdentityIsAutoIncrementingAndNotCompulsory_MultipleProps()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            DMProperty property1 = dmClass.Properties.CreateBusinessObject();
            DMProperty property2 = dmClass.Properties.CreateBusinessObject();
            DMObjectIdentity dmObjectIdentity = dmClass.CreateObjectIdentity(property1);
            dmObjectIdentity.Properties.Add(property2);
            property1.AutoIncrementing = true;
            property1.Compulsory = false;
            property2.AutoIncrementing = true;
            property2.Compulsory = false;
            IBusinessObjectRule rule = new DMObjectIdentityAllPropsCompulsoryRule();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmClass.ObjectIdentity);
            Assert.AreEqual(2, dmObjectIdentity.Properties.Count);
            Assert.IsTrue(property1.AutoIncrementing.Value);
            Assert.IsFalse(property1.Compulsory.Value);
            Assert.IsTrue(property2.AutoIncrementing.Value);
            Assert.IsFalse(property2.Compulsory.Value);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmObjectIdentity);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
        }

        #endregion
    }

}